﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Net.Sockets;


namespace LinnCorLib.Net
{
    public class Client
    {
        public Socket clientSocket { get; private set; }
        private ChatServer server;
        public string ID { get; private set; }

        private string strReceiveMsg = "";

        public Client(string id, Socket socket, ChatServer server)
        {
            this.ID = id;
            this.clientSocket = socket;
            this.server = server;

            Receive();
        }

        private void Receive()
        {
            byte[] buff = new byte[4096];
            clientSocket.BeginReceive(buff, 0, 4096, SocketFlags.None, new AsyncCallback(ReceiveCallBack), buff);
        }

        private void ReceiveCallBack(IAsyncResult asyncResult)
        {
            try
            {
                byte[] Ibuff = (byte[])asyncResult.AsyncState;
                int recv = clientSocket.EndReceive(asyncResult);

                if (recv == 0)
                {
                    Disconnect();
                    return;
                }
                else
                {
                    strReceiveMsg = Encoding.Default.GetString(Ibuff, 0, recv);
                    Console.WriteLine("strReceiveMsg : {0}", strReceiveMsg);

                    server.ReceiveMessage(strReceiveMsg);

                    Receive();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Client Method : {0}\n{1}", "ReceiveCallBack", e.Message);
                //eyChatServer.Add_Log(e.Message);
                Disconnect();
            }
        }

        public void ClientSend(string msg)
        {
            try
            { 
                byte[] Ibuff = Encoding.Default.GetBytes(msg);
                clientSocket.BeginSend(Ibuff, 0, Ibuff.Length, SocketFlags.None, new AsyncCallback(SendCallBack), Ibuff);
            }
            catch (Exception e)
            {
                Console.WriteLine("Client Method : {0}\n{1}", "ClientSend", e.Message);
                Disconnect();
            }
        }

        private void SendCallBack(IAsyncResult asyncResult)
        {
            try
            {
                int send = clientSocket.EndSend(asyncResult);
                if (send == 0)
                    Disconnect();
                 
                Receive();
            }
            catch (Exception e)
            {
                Console.WriteLine("Client Method : {0}\n{1}", "SendCallBack", e.Message);
                Disconnect();
            }
        }

        private void Disconnect()
        {
            if (clientSocket != null)
            {
                clientSocket.Close();
                clientSocket = null;
            }

            server.RemoveClient(this.ID);
        }
    }
}
